clear
use  ${data}teacher_data.dta

* Only observe performance outcomes between 2011 and 2014 in Chicago region
keep if CorpsYear>=2011 & CorpsYear<=2014 & region==1

* ------------------------------------------------ *
* Table A10: Impact of DA on Teachers' Performance *
* ------------------------------------------------ *


 eststo clear


 
foreach c in hasBa1  ba1 ba_trunc1  hasBa2  ba2 ba_trunc2 {

	reg `c'  da   i.hs i.CorpsYear  $X , cluster(region_gl)
	eststo `c'
	estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): `c'
	qui su `c' if da==1 & region==1
	estadd scalar cm = round(r(mean) - _b[da],.001): `c'
	
	
 }


forv y=1/2 {
 foreach c in ba`y' ba_trunc`y' {
	
	reg `c'  da   i.hs i.CorpsYear ba_typeMissing1 ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y' $X , cluster(region_gl)
	eststo `c'_extraX
	estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): `c'_extraX
	qui su `c' if da==1 & region==1
	estadd scalar cm = round(r(mean) - _b[da],.001): `c'_extraX

 }
}


	 
* Multiple Imputation

forv y=1/2 {


* First, use untruncated measure 
preserve	

	
#delimit ;
keep ba`y' ba_typeMissing`y' ba_typeState`y'  step1  step2 step4 
	completedCorps da hs CorpsYear $X region_gl   CorpsYear $X region_gl 
ba_typeMissing`y' ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y';


mi set flong;

/* Register Which Variables Will and Will Not Be Imputed */
mi register imputed  ba`y' ;
mi register regular step1  step2 step4 
	completedCorps da hs CorpsYear $X region_gl 
	ba_typeMissing`y' ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y';

/* Impute  */
 mi impute chained (pmm, knn(5))  ba`y'	 = step1  step2 step4
	completedCorps  da hs CorpsYear $X , by(region_gl) add(20) rseed(13295555);
									 
mi estimate, cmdok post: reg ba`y' da i.hs  i.CorpsYear $X , cluster(region_gl);
eststo ba`y'_mi;
estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): ba`y'_mi;
mi estimate, post: mean ba`y' if da==1 ;
matrix b=e(b);
local mean = b[1,1];
estadd scalar cm = round(`mean'-_b[da], .001) : ba`y'_mi;

	
mi estimate, cmdok post: reg ba`y' da i.hs  i.CorpsYear $X ba_typeMissing`y' ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y', cluster(region_gl);
eststo ba`y'_mi_extraX;
estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): ba`y'_mi_extraX;
mi estimate, post: mean ba`y' if da==1 ;
matrix b=e(b);
local mean = b[1,1];
estadd scalar tm = round(`mean', .001) : ba`y'_mi_extraX;
estadd scalar cm = round(`mean'-_b[da], .001) : ba`y'_mi_extraX;






* Re-do using truncated performance measure ;	 
restore ;




preserve;



keep ba_trunc`y' ba_typeMissing1 ba_typeState1  step1  step2 step4 
completedCorps  da hs  CorpsYear $X region_gl 
ba_typeMissing`y' ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y';



/* See "help mi styles" */
mi set flong;

/* Register Which Variables Will and Will Not Be Imputed */
mi register imputed  ba_trunc`y' ;
mi register regular step1  step2 step4 
	completedCorps da hs CorpsYear $X region_gl 
	ba_typeMissing`y' ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y';

/* Impute  */
 mi impute chained (pmm, knn(5))  ba_trunc`y' = step1  step2 step4
	completedCorps  da hs  CorpsYear $X  , by(region_gl) add(20) rseed(91555306);
									 
mi estimate, cmdok post: reg ba_trunc`y' da i.hs  i.CorpsYear $X , cluster(region_gl);
eststo ba_trunc`y'_mi;
estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): ba_trunc`y'_mi;
mi estimate, post: mean ba_trunc`y' if da==1 ;
matrix b=e(b);
local mean = b[1,1];
estadd scalar cm = round(`mean'-_b[da], .001) : ba_trunc`y'_mi;



	 
mi estimate, cmdok post: reg ba_trunc`y' da i.hs  i.CorpsYear $X ba_typeMissing`y' ba_typeState`y' ba_typeTeacherSchool`y' ba_typeTFA`y' ba_typeOther`y' ba_sourceCalc`y' ba_sourceStandard`y' ba_sourceOther`y' ba_sourceMissing`y', cluster(region_gl);
eststo ba_trunc`y'_mi_extraX;
estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): ba_trunc`y'_mi_extraX;
mi estimate, post: mean ba_trunc`y' if da==1 ;
matrix b=e(b);
local mean = b[1,1];
estadd scalar cm = round(`mean'-_b[da], .001) : ba_trunc`y'_mi_extraX;
	 


	 
	 

	 
	 
 #delimit cr

 restore
}	 
	 

	
	 
estout hasBa1 ba1  ba1_mi  ba_trunc1  ba_trunc1_mi ba1_extraX ba1_mi_extraX  ba_trunc1_extraX ba_trunc1_mi_extraX  using ${results}tableA9.txt, replace ///
	keep(da) cells(b(star fmt(3) nostar)  se(par fmt(3))) ///
	collabels(,none) stat(p cm  N )  	
	
estout hasBa2 ba2  ba2_mi  ba_trunc2  ba_trunc2_mi ba2_extraX ba2_mi_extraX  ba_trunc2_extraX ba_trunc2_mi_extraX  using ${results}tableA9.txt, append ///
	keep(da) cells(b(star fmt(3) nostar)  se(par fmt(3))) ///
	collabels(,none) stat(p cm  N )  	

	

	
	
* ---------------------------------------*
* Figure A2: Performance Parallel Trends *
* -------------------------------------- *
	

forv y=1/2 {
	
clear
use  ${data}teacher_data.dta

* Only observe performance outcomes between 2011 and 2014 in Chicago region
keep if CorpsYear>=2011 & CorpsYear<=2014 & region==1
	

reg ba`y' da2011 da2012 da2014  i.hs i.CorpsYear  $X  , cluster(region_gl)	


matrix coef = J(4,3,.)
local row=0
foreach year in  2011 2012   { 
	local row=`row'+1
	matrix coef[`row',1] = `year'
	matrix coef[`row',2] = _b[da`year']
	matrix coef[`row',3] = _se[da`year']
}
matrix coef[3,1] = 2013
matrix coef[3,2] = 0
matrix coef[3,3] = 0
matrix coef[4,1] = 2014
matrix coef[4,2] = _b[da2014]
matrix coef[4,3] = _se[da2014]
	
clear
svmat coef
ren coef1 cohort
ren coef2 effect

* Confidence intervals use 1 degree of freedom
gen lb = effect - 12.7062*coef3
gen ub = effect + 12.7062*coef3

		
graph twoway (connected effect cohort, lcolor(black) mcolor(black)) (rcap ub lb cohort, lcolor(black)), xline(2013.1, lcolor(black) lpattern(dash))    legend(label(1 "Effect")  label(2 "95% Confidence Interval") pos(6) cols(1)) graphregion(color(white)) xtitle("Cohort") ytitle("HS-Elementary Difference") 

graph export ${results}figureA2_year`y'.png, replace
	
}	
	
	
exit	
